package test3;

import java.util.Arrays;

class Node{
    public int val;
    public Node left;
    public Node right;

    public Node(int val) {
        this.val = val;
    }
}

public class BinarySearchTree {

    public Node root=null;

    //查找某个节点
    public Node search(int key){
        Node cur=root;
        while(cur != null){
            if(cur.val < key){
                cur=cur.right;
            }else if(cur.val > key){
                cur=cur.left;
            }else{
                return cur;
            }
        }
        return null;
    }

    //插入元素
    public boolean insert(int val) {
        if (root == null) {
            root = new Node(val);
            return true;
        }
        Node parent = null;
        Node cur = root;
        Node node = new Node(val);
        while (cur != null) {
            if (cur.val < val) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val == val){
                return false;
            } else {
                parent = cur;
                cur = cur.left;
            }
        }
        if (parent.val < val) {
            parent.right = node;
        } else {
            parent.left = node;
        }
        return true;
    }

    //删除
    public void remove(int key){
        Node cur=root;
        Node parent=null;
        while(cur != null){
            if(cur.val==key){
                removeNode(cur,parent);
                break;
            }else if(cur.val < key){
                parent=cur;
                cur=cur.right;
            }else{
                parent=cur;
                cur=cur.left;
            }

        }
    }

    public void removeNode(Node cur,Node parent){
        if(cur.left==null ){
            if(cur==root){
                root=cur.right;
            }else if(cur==parent.left){
                parent.left=cur.right;
            }else{
                parent.right=cur.right;
            }
        }else if(cur.right==null){
            if(cur==root){
                root=cur.left;
            }else if(cur==parent.left){
                parent.left=cur.left;

            }else{
                parent.right=cur.left;
            }
        }else{
            Node ta=cur.right;
            Node tap=cur;
            while(ta.left != null){
                tap=ta;
                ta=tap.left;
            }
            cur.val=ta.val;
            if(ta==tap.left){
                tap.left=ta.right;
            }else{
               tap.right=ta.right;
            }

        }
    }

    public void inOrder(Node root){
        if(root == null)  return;
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    public static void main(String[] args) {
        int[] array={10,8,19,2,3,9,4,7};
        BinarySearchTree binarySearchTree=new BinarySearchTree();
        for (int i=0;i<array.length;i++){
            binarySearchTree.insert(array[i]);
        }
        binarySearchTree.inOrder(binarySearchTree.root);
        System.out.println("插入重复的数据");
        System.out.println(binarySearchTree.insert(3));
        System.out.println("删除数据");
        binarySearchTree.remove(7);
        System.out.println();
        binarySearchTree.inOrder(binarySearchTree.root);

    }

}
